package com.biovr.backend.service;

import com.biovr.backend.domain.Reagent;
import com.biovr.backend.repository.ReagentRepository;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.QrCodeUtils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by monodev on 2017/5/8.
 */
@Service
public class ReagentServiceImpl implements ReagentService {
    @Autowired
    private ReagentRepository reagentRepository;
    @Autowired
    private DateTimeUtils dateTimeUtils;
    @Autowired
    private QrCodeUtils qrCodeUtils;
    @Value("${tbname.reagent}")
private String tbName;
    /**
     * 增加试剂
     * @param request
     * @param  reagent 试剂，必填
     * @return
     */
    public Reagent addReagent(HttpServletRequest request, Reagent reagent) {
        reagent.setTableName(tbName);
        reagent.setStatus(true);
        reagent.setCreateTime(dateTimeUtils.getCurrDateTimeString());
        reagent = reagentRepository.save(reagent);
        String qrcode = generateQrcode(request,reagent);
        reagent.setQCode(qrcode);
        reagent= reagentRepository.save(reagent);
        return reagent;
    }

    /**
     * 重新生成二维码
     * @param request
     * @param apparatusid
     * @return
     */
    public String regenerateQrcode(HttpServletRequest request, String apparatusid) throws Exception{
        Reagent reagent = reagentRepository.findOne(apparatusid);
        if(null!=reagent){
            qrCodeUtils.deleteQrcode(reagent.getQCode());
            String qrcode = generateQrcode(request,reagent);
            reagent.setQCode(qrcode);
            reagentRepository.save(reagent);
            return reagent.getQCode();
        }
        return null;
    }
    /**
     * 生成二维码
     * @param request
     * @param reagent
     * @return
     */
    private String generateQrcode(HttpServletRequest request, Reagent reagent){
        //生成二维码
        //获得根请求地址
//        int port= -1;
//        port=request.getLocalPort();
        //String basePath = request.getScheme()+"://"+request.getServerName()+(port==-1?"/":":"+port+"/");
        String basePath = request.getScheme()+"://"+request.getServerName()+"/";
        //根据rest规则生成链接
        String qCodeContent = basePath+"reagents/"+reagent.getId();
        try {
            //这是base64编码二维码，因生成的二维码数据量太大，已弃用
            // newExpm.setQCode(qrCodeUtils.getBase64QRCode(qCodeContent));
            String qCodeFileName = UUID.randomUUID().toString().replace("-","")+".jpg";
            qrCodeUtils.saveQRCodeToFile(qCodeContent,qCodeFileName,null);
            //reagent.setQCode(qCodeFileName);
            return qCodeFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据ID删除
     * @param id
     */
    public  boolean deleteReagent(String id) throws Exception{
        if (reagentRepository.exists(id))
        {
            Reagent e = reagentRepository.findOne(id);
            qrCodeUtils.deleteQrcode(e.getQCode());
            reagentRepository.delete(e);
            return true;
        }
        return  false;
    }
}
